package com.imc.core.config.mvc;

import com.imc.platform.base.user.security.ApiSecurityFilter;
import com.imc.core.listener.SessionCount;
import feign.codec.Decoder;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.springframework.beans.factory.ObjectFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.web.HttpMessageConverters;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.MultipartConfigFactory;
import org.springframework.boot.web.servlet.ServletListenerRegistrationBean;
import org.springframework.cloud.netflix.feign.support.SpringDecoder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.MediaType;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

import javax.servlet.ServletContext;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by tanweiping on 2018/7/23.
 */
/*
* 类描述：springmvc相关的配置
* 开发者：tanweiping
* 创建日期：2018/8/7 10:05
*/
@EqualsAndHashCode(callSuper = false)
@Data
@ConfigurationProperties(prefix = "server")
@Configuration
public class MvcConfigurerAdapter extends WebMvcConfigurerAdapter  {

    private String host;
    private String contextPath;
    private int port;
    private String staticUrl;
    private String siteUrl;
    private String loginUrl;
    private String indexUrl;
    //private String frameUrl;

    @Autowired
    private ServletContext servletContext;

    /*
    * tanweiping  2018/8/7 10:24
    * 方法描述：配置映射
    */
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/");
        registry.addResourceHandler("/index/**").addResourceLocations("classpath:/templates/");
        registry.addResourceHandler("/login/**").addResourceLocations("classpath:"+loginUrl);
        //registry.addResourceHandler("/index/**").addResourceLocations("classpath:"+indexUrl);
        //registry.addResourceHandler("/frameUrl/**").addResourceLocations("classpath:"+frameUrl);

        servletContext.setAttribute("staticUrl",staticUrl);
        servletContext.setAttribute("siteUrl",siteUrl);
        servletContext.setAttribute("contextPath",contextPath);
    }

//    @Bean
//    public FilterRegistrationBean apiFilterRegistration() {
//        FilterRegistrationBean apiFilterRegistration = new FilterRegistrationBean(new ApiSecurityFilter());
//        apiFilterRegistration.addUrlPatterns("/api/public/api/wx/*");
//        apiFilterRegistration.addInitParameter("reqHander","/api/wx");
//        apiFilterRegistration.addInitParameter("noFilter","/api/wx/index/getOpenIdByCode");
////        apiFilterRegistration.setName("");
//        return apiFilterRegistration;
//    }

    @Bean
    public ServletListenerRegistrationBean<SessionCount> serssionListenerBean(){
        ServletListenerRegistrationBean<SessionCount>
                sessionListener = new ServletListenerRegistrationBean<SessionCount>(new SessionCount());
        return sessionListener;
    }

//    @Override
//    public void addInterceptors(InterceptorRegistry registry) {
//        registry.addInterceptor(errorPageInterceptor);
//        super.addInterceptors(registry);
//    }

//    @Bean
//    public Decoder feignDecoder(){
//        ObjectFactory<HttpMessageConverters> objectFactory = () -> new HttpMessageConverters(new WxMessageConverter());
//        return new SpringDecoder(objectFactory);
//    }
}
